package com.itheima.d5_Api_BigDecimal;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class demo {
    public static void main(String[] args) {
        //浮点运算会出现失真问题
        System.out.println(0.09+0.01);
        System.out.println(1.0-0.32);
        System.out.println(1.015*100);
        /**
         * 0.09999999999999999
         * 0.6799999999999999
         * 101.49999999999999
         */
        double a = 0.1;
        double b = 0.2;
        double c = a +b ;
        System.out.println(c);
        BigDecimal a1 =BigDecimal.valueOf(a);
        BigDecimal a2 =BigDecimal.valueOf(b);
        BigDecimal a3 = a1.add(a2);
        System.out.println(a3);

        //将a3转换为double类型，类似于StringBuilder
        double rs = a3.doubleValue();
        System.out.println(rs);

        //需要精度运算
        BigDecimal a11 =BigDecimal.valueOf(10.0);
        BigDecimal a22 =BigDecimal.valueOf(3.0);
        //System.out.println(a11.divide(a22));//会报错
        System.out.println(a11.divide(a22,2,RoundingMode.HALF_UP));//保留两位小数，四舍五入

    }
}
